package com.cyw.thread.create;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Author: Chenyw
 * @Date: 2022/09/20/19:31
 */
class Thread01 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0){
                System.out.println(Thread.currentThread().getName()+"偶数:"+i);
            }
        }
    }
}
class Thread02 implements Runnable{
    @Override
    public void run()
    {
        for(int i=0;i<100;i++)
        {
            if(i%2==0)
            {
                System.out.println("偶数："+i);
            }
        }
    }
}

class Thread03 implements Callable {
    @Override
    public Object call() throws Exception
    {
        int sum=0;
        for(int i=1;i<=100;i++)
        {
            //判断是偶数
            if(i%2==0)
            {
                sum=sum+i;
            }
        }
        //自动装箱
        return sum;
    }
}

@SuppressWarnings("all")
public class ThreadFunction {
    public static void main(String[] args) {
        /**
         * 继承Thread
         */
        new Thread01().start();

        /**
         * 实现Runnable
         */
        new Thread(new Thread02()).start();


        /**
         * 实现Callable
         */
        FutureTask futureTask = new FutureTask(new Thread03());
        new Thread(futureTask).start();
        try
        {
            //get方法只是为了返回call方法的返回值。
            //get方法自动调用newThread类对象的call()方法
            Object sum = futureTask.get();
            System.out.println(sum);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        } catch (ExecutionException e)
        {
            e.printStackTrace();
        }


        new Thread(){
            @Override
            public void run() {
                for(int i=0;i<100;i++) {
                    //判断是奇数
                    if(i%2!=0) {
                        System.out.println(Thread.currentThread().getName()+"匿名奇数："+i);
                    }
                }
            }
        }.start();
    }
}